WHICH

Section: User Commands (1)
Index Return to Main Contents
 

NAME

which - give alias, function or path expansion of command  

SYNOPSIS

which [ -i ] [ -a ] [ -- ] [ command ]  

DESCRIPTION

Which provides the user with the full expansion of the command argument, be it either an alias, a shell function, or an executable file (default). To enable search for aliases and shell functions the user should supply the -i (interactive) flag. In that case which expects as standard input the expansion of the alias or shell function. If the standard input is empty or the -i flag has not been given, which will try to locate command in the user's PATH. The interactive mode is easily used by setting an alias like the following:

alias which alias !\$ \| /usr/local/bin/which -i !\*

in csh, or

alias which eval alias '\"\$$#\" |' \
        /usr/local/bin/which -i '${1+"$@"}'

in shells which are supersets of sh and which know aliases. If your shell has shell functions, you can use the following function:

which()
{
        eval last=\"\$$#\"
        set | sed -n "/^$last(){$/,/^}$/p" |
                /usr/local/bin/which -i ${1+"$@"}
}

If the -a (all) flag is given, which will not stop after the first `match', but search for all occurrences of command in the user's PATH. The -- flag can be used to end the list of options: the next argument (if present) will be taken as command, even if it starts with a `-'. which [-i] [-a] [--] without further arguments prints the user's PATH broken up into its components, one per line.

This new version of the which command is not a csh script. Being an executable it is much faster, and not sourcing .cshrc it gives a true picture of one's aliases. Furthermore it will give the correct answers even if:

-
the effective uid (gid) differs from the real uid (gid)
-
the effective uid is 0 (root).
 

EXAMPLE

% alias
which   alias !$ | /usr/local/bin/which -i !*
% which which
which   alias !$ | /usr/local/bin/which -i !*
% which -a which
which   alias !$ | /usr/local/bin/which -i !*
/usr/local/bin/which
/usr/ucb/which
%
 

AUTHOR

Maarten Litmaath @ VU Informatika Amsterdam


 

Index

NAME
SYNOPSIS
DESCRIPTION
EXAMPLE
AUTHOR

This document was created by man2html, using the manual pages.
Time: 03:58:28 GMT, September 10, 2022